<?php
    /**
    3  * OrangeHRM Enterprise is a closed sourced comprehensive Human Resource Management (HRM)
    4  * System that captures all the essential functionalities required for any enterprise.
    5  * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
    6  *
    7  * OrangeHRM Inc is the owner of the patent, copyright, trade secrets, trademarks and any
    8  * other intellectual property rights which subsist in the Licensed Materials. OrangeHRM Inc
    9  * is the owner of the media / downloaded OrangeHRM Enterprise software files on which the
   10  * Licensed Materials are received. Title to the Licensed Materials and media shall remain
   11  * vested in OrangeHRM Inc. For the avoidance of doubt title and all intellectual property
   12  * rights to any design, new software, new protocol, new interface, enhancement, update,
   13  * derivative works, revised screen text or any other items that OrangeHRM Inc creates for
   14  * Customer shall remain vested in OrangeHRM Inc. Any rights not expressly granted herein are
   15  * reserved to OrangeHRM Inc.
   16  *
   17  * You should have received a copy of the OrangeHRM Enterprise  proprietary license file along
   18  * with this program; if not, write to the OrangeHRM Inc. 538 Teal Plaza, Secaucus , NJ 0709
     * to get the file.
     *
     */
    require_once ROOT_PATH . '/plugins/plugInCommon.php';
    require_once ROOT_PATH . '/lib/dao/DMLFunctions.php';
    require_once ROOT_PATH . '/lib/dao/SQLQBuilder.php';
    require_once 'LdapDetails.php';
    require_once 'LogFileWriterLdap.php';
    
    class LdapLogin extends plugInCommon {
    	public function __construct() {
    		$this->setPlugInName('LDAP');
    	}
    	public function ldapAuth($userName, $passWord) {
    		//error_log("dfgdfg",3,"C:/logtest.log");
    	    $ldapDetails = new LdapDetails();
    		$ldapDetails->retrieveLdapDetails();
    		$ldapHost = "ldap://" .$ldapDetails->getLdapServer();
    	    $ldapPort = $ldapDetails->getLdapPort();
    	     //error_log($ldapHost,3,"C:/logtest.log");
    		if (!empty($ldapPort)){
    			$ldapConnection =@ ldap_connect($ldapHost, $ldapDetails->getLdapPort());
    		} else {
    			$ldapConnection =@ ldap_connect($ldapHost);
    		}
    		if (!$ldapConnection)
    			LogFileWriterLdap::logLdapError('LDAP connect failed', $userName, $ldapConnection);
    	    ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3);
    	    //$ldapUser = "uid=" . $userName . "," . $ldapDetails->getLdapDomainName();
    	    $ldapUser = $userName . "@" . $ldapDetails->getLdapDomainName();   	    
    	    $ldapAuthResult =@ ldap_bind($ldapConnection, $ldapUser, $passWord);
    		if (!$ldapAuthResult)
    			LogFileWriterLdap::logLdapError('LDAP bind failed', $userName, $ldapConnection);
    		return $ldapAuthResult;
    
    	}
    
    	public function ldapAuthWithoutPass($username)
    	{    		
    		
    		$sqlBuilder = new SQLQBuilder();
    		$selectFields[0] = '`emp_work_email`';
    		$selectTable = '`hs_hr_employee`';
    		$selectConditions[0] = 'emp_work_email like "%'.$username.'%"';
    		
    		$query = $sqlBuilder->simpleSelect($selectTable, $selectFields, $selectConditions);
    		
    		#die($query);
    		$dbConnection = new DMLFunctions();
    		$result = $dbConnection->executeQuery($query);
    		$array = $dbConnection->dbObject->getArray($result);
    		/*print_r($array);
    		die();*/
    		if(count($array) > 0)
    		{    
    			#die('go here');			
    			return true;
    		}
    		return false;
    	}
    	
    	public function retrieveLdapStatus() {
    
    		$sqlBuilder = new SQLQBuilder();
   		$selectFields[0] = '`value`';
    		$selectTable = '`hs_hr_config`';
    		$selectConditions[0] = "`key` = 'ldap_status'";
    		$query = $sqlBuilder->simpleSelect($selectTable, $selectFields, $selectConditions);
    		$dbConnection = new DMLFunctions();
    		$result = $dbConnection->executeQuery($query);
    		$row = $dbConnection->dbObject->getArray($result);
    		$ldapStatus = $row['value'];
    		return $ldapStatus;
    
    	}
 	 }
    ?>
